import 'package:city_pickers/city_pickers.dart';
import 'package:city_pickers/modal/result.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mypeiwan/pages/moreitemspage.dart';
import 'package:mypeiwan/pages/peiwaninfodetail.dart';
import 'package:mypeiwan/widgets/peiwaninfo.dart';
import 'package:mypeiwan/widgets/peiwanitem.dart';

import '../utils/province.dart';

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _HomePageState();
  }
}

class _HomePageState extends State<HomePage> {
  String? positionName = "选择位置";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    positionName = "选择位置";
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
        child: ListView(
          shrinkWrap: true,
          children: [
            Row(
              children: [
                InkWell(
                  child: Text(positionName!),
                  onTap: () {
                    getResult();
                  },
                ),
                Container(
                  width: 200,
                  child: Expanded(
                    child: Row(
                      children: [
                        Icon(Icons.search_rounded),
                        Expanded(
                          child: TextField(
                            decoration: InputDecoration(
                                border: OutlineInputBorder(),
                                hintText: '搜索id和'),
                          ),
                        )
                      ],
                    ),
                  ),
                )
              ],
            ),
            Container(
              clipBehavior: Clip.hardEdge,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(10))),
              child: Image.asset(
                'images/banner.png',
                fit: BoxFit.cover,
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(0, 20, 0, 0),
              child: Row(
                children: [
                  Expanded(
                    child: PeiwanItem(
                        itemName: "逛街看电影",
                        icon: Image.asset("images/movie_icon.png")),
                  ),
                  SizedBox(
                    width: 20,
                  ),
                  Expanded(
                    child: PeiwanItem(
                        itemName: "美食探店",
                        icon: Image.asset("images/food_icon.png")),
                  ),
                  SizedBox(
                    width: 20,
                  ),
                  Expanded(
                    child: PeiwanItem(
                        itemName: "密室剧本杀",
                        icon: Image.asset("images/jubensha_icon.png")),
                  ),
                  SizedBox(
                    width: 20,
                  ),
                  Expanded(
                    child: PeiwanItem(
                        itemName: "体育运动",
                        icon: Image.asset("images/sport_icon.png")),
                  ),
                  SizedBox(
                    width: 20,
                  ),
                  Expanded(
                    child: InkWell(
                      child: PeiwanItem(
                          itemName: "更多分类",
                          icon: Image.asset("images/more_icon.png")),
                      onTap: () {
                        Navigator.of(context).push(
                          MaterialPageRoute(
                            builder: (context) => MoreItemsPage(),
                          ),
                        );
                      },
                    ),
                  ),
                ],
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Expanded(
                  child: Image.asset(
                    "images/commend_icon.png",
                    fit: BoxFit.cover,
                  ),
                ),
                Expanded(
                    child: Text(
                  "推荐达人",
                  style: TextStyle(fontSize: 8),
                )),
                SizedBox(
                  width: 200,
                ),
                Expanded(
                    child: Text(
                  "查看更多",
                  style: TextStyle(fontSize: 8),
                )),
                Expanded(
                  child: IconButton(
                    icon: Icon(Icons.arrow_right),
                    onPressed: () {},
                  ),
                ),
              ],
            ),
            GridView.count(
              mainAxisSpacing: 20,
              crossAxisSpacing: 10,
              shrinkWrap: true,
              physics: new NeverScrollableScrollPhysics(),
              crossAxisCount: 2,
              children: List.generate(10, (index) {
                return InkWell(
                  child: PeiwanInfo(
                      true,
                      1,
                      "名字${index}",
                      20,
                      167,
                      0.44,
                      ["王者荣耀", "和平精英"],
                      AssetImage("images/peiwan_picture.png")),
                  onTap: () {
                    Navigator.of(context).push(
                      MaterialPageRoute(
                        builder: (context) => PeiwanInfoDetail(index),
                      ),
                    );
                  },
                );
              }),
            )
          ],
        ),
      ),
    );
  }

  Future<Result?> getResult() async {
    Result? result = await CityPickers.showFullPageCityPicker(
      context: context,
      provincesData: provincesData,
    );
    setState(() {
      positionName = result?.areaName!;
    });
    print('result $result');
    return result;
  }
}
